package com;
import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;



@Path("helloworld")
public class smi
{
	@GET
	@Produces("application/pdf")
	public Response getmessage(@QueryParam("json") String s)
	{
		int i =0;
		//SessionFactory f=new Configuration().configure().buildSessionFactory();
		//Session s=f.openSession();
		System.out.println(s);
		System.out.println("welcome parthesh");
        
		String st = s;
		  String[] temp;
		 
		 
		  String delimiter = ",";
		 
		  temp = st.split(delimiter);
		   for( ; i < temp.length; i++)
		   {
		    System.out.println(temp[i]);	
		   }
		
		
		   
		   
		Document doc=new Document();
		ByteArrayOutputStream ba=new ByteArrayOutputStream();
		try {
			//ManyToOne mo=new ManyToOne();
			//process s1;
			List<process> list=main.viewRecord(temp);
			
			System.out.println(list.size());
			
			PdfWriter writer=PdfWriter.getInstance(doc, ba);
//			com.lowagie.text.Image image = com.lowagie.text.Image
//					.getInstance("c:/smi-logo.gif");
//					image.setBorder(1);
//					image.scaleAbsolute(100, 100);
//					image.setAbsolutePosition(450, 730);
//					writer.addDirectImageSimple(image);
//			
			
			doc.open();
			 PdfPTable headerTable= new PdfPTable(2);
			    Image ii = Image.getInstance("c:/smi-logo.gif");
			    ii.setAlignment(Element.ALIGN_RIGHT);
			    ii.scalePercent(25);
			 
			    PdfPCell cell1 = new PdfPCell(ii,false);
			    cell1.setBorder(0);
			    
			    headerTable.addCell(cell1);
			 
			 //   cell = borderlessCell("Code of Doom Order Form");
			    

				

Date now = new Date();
System.out.println("date--> " + now);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String s1 = df.format(now);
			    
Chunk CONNECT = new Chunk(new LineSeparator(5.5f, 95, Color.BLUE, Element.ALIGN_CENTER, 3.5f));
  //  LineSeparator UNDERLINE =new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);

//public HeaderFooter getMyHeader(String titulo) throws ServletException {  
//    Phrase p= new Phrase();  
//    Chunk chunkTemp = new Chunk("My Company Name\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.NORMAL);  
//    p.add(chunkTemp);  
//    chunkTemp = new Chunk("_____________________________________________________________________________________________________", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.NORMAL);  
//    p.add(chunkTemp);  
//    HeaderFooter header = new HeaderFooter(p, false);  
//    return header;  



			    headerTable.addCell(main.borderlessCell(s1));
			 
//			    headerTable.addCell(borderlessCell("Customer Name"));
//			    headerTable.addCell(borderlessCell(c.getName()));
//			    headerTable.addCell(borderlessCell("Address"));
//			    headerTable.addCell(borderlessCell(c.getAddress()));
//			    headerTable.addCell(borderlessCell("Phone Number"));
//			    headerTable.addCell(borderlessCell(c.getPhone()));
			    doc.add(headerTable);
//	            Phrase phrase = new Phrase(50);
//	            doc.add(phrase);
			    Paragraph paragraph = new Paragraph( "");
			    main.addEmptyLine(paragraph,10);
			   paragraph.add(CONNECT);
			    doc.add( paragraph );  
			    
//			    Chunk c = new Chunk( text, font );
//			    c.setAction( new PdfAction( new URL( url ) ) );
//			    c.setUnderline(0.8f, -0.8f);
//			    doc.add( c );
			    
			    
			    
			    
			    PdfPTable tab=new PdfPTable(6);
				PdfPCell cell = new PdfPCell(new Paragraph("Process Details"));
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setBackgroundColor(new Color(20,105,160));
				cell.setColspan(6);
				tab.addCell(cell);
				tab.addCell("id");
				tab.addCell("pid");
				tab.addCell("mid");
				tab.addCell("processname");
				tab.addCell("workingsetsize");
				tab.addCell("date");
				doc.add(tab);
				
			
			
			
			PdfPTable table=new PdfPTable(i);
			Iterator it =list.iterator();
            while(it.hasNext())
            {
            	Object o[]=(Object [])it.next();
            	
       	     //                   System.out.println(o[0] + "==" + o[1]);
                  
               //  String str=(String) it.next();
                 for(int j=0; j<i; j++ )
                 {
                	 System.out.println(o[j]);
  	               
				//table.addCell(o[j]);//String.valueOf(list.get(i).getProcessName()));
				table.addCell(String.valueOf(o[j]));
				
			}} 
            System.out.println("loop complate");
			doc.add(table);
			System.out.println("add table complate");
			
			doc.close();
			System.out.println("document complate");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ByteArrayInputStream byteArray=new ByteArrayInputStream(ba.toByteArray());
		System.out.println("bytearray");
		
		return  Response.ok().entity(byteArray).header("Content-Disposition","attachment; filename=hello.pdf").type("application/pdf").build();

	
	//	return "ok";
		
	}
	
	
	
	
}
/*
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
 
import part1.chapter02.MovieHistory;
import part1.chapter02.MovieLinks1;
 
public class ConcatenateStamp {
 
    public static final String RESULT
        = "results/part2/chapter06/concatenated_stamped.pdf";
 
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
    	// use old examples to create PDFs
        MovieLinks1.main(args);
        MovieHistory.main(args);
        // step 1
        Document document = new Document();
        // step 2
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        // reader for document 1
        PdfReader reader1 = new PdfReader(MovieLinks1.RESULT);
        int n1 = reader1.getNumberOfPages();
        // reader for document 2
        PdfReader reader2 = new PdfReader(MovieHistory.RESULT);
        int n2 = reader2.getNumberOfPages();
        // initializations
        PdfImportedPage page;
        PdfCopy.PageStamp stamp;
        // Loop over the pages of document 1
        for (int i = 0; i < n1; ) {
            page = copy.getImportedPage(reader1, ++i);
            stamp = copy.createPageStamp(page);
            // add page numbers
            ColumnText.showTextAligned(
                    stamp.getUnderContent(), Element.ALIGN_CENTER,
                    new Phrase(String.format("page %d of %d", i, n1 + n2)),
                    297.5f, 28, 0);
            stamp.alterContents();
            copy.addPage(page);
        }
        // Loop over the pages of document 2
        for (int i = 0; i < n2; ) {
            page = copy.getImportedPage(reader2, ++i);
            stamp = copy.createPageStamp(page);
            // add page numbers
            ColumnText.showTextAligned(
                    stamp.getUnderContent(), Element.ALIGN_CENTER,
                    new Phrase(String.format("page %d of %d", n1 + i, n1 + n2)),
                    297.5f, 28, 0);
            stamp.alterContents();
            copy.addPage(page);
        }
        // step 5
        document.close();
    }
}


*/
